<?php

class TeacherAction extends CommonAction{

	function lists(){
		if($_REQUEST['username'] !=null && $_REQUEST['username'] != "" ){
			$data['username'] = array('like','%'.$_REQUEST['username'].'%');
			$real['username'] = $_REQUEST['username'];
			$this->assign('username',$_REQUEST['username']);
		}
		if($_REQUEST['shenhe'] !=null && $_REQUEST['shenhe'] != "" ){
			$data['shenhe'] = $_REQUEST['shenhe'];
			$real['shenhe'] = $_REQUEST['shenhe'];
			$this->assign('shenhe',$_REQUEST['shenhe']);
		}
		
		if($_REQUEST['SJHM'] !=null && $_REQUEST['SJHM'] != "" ){
			$data['SJHM'] = array('like','%'.$_REQUEST['SJHM'].'%');
			$real['SJHM'] = $_REQUEST['SJHM'];
			$this->assign('SJHM',$_REQUEST['SJHM']);
		}

		 if($_REQUEST['school_id'] != null && $_REQUEST['school_id'] != ""){
		 	$showschool = $this->selectschool($_REQUEST['school_id']);
			$real['school_id'] = $_REQUEST['school_id'];
		 	//学校ID
		 	$tt['school_id'] = $_REQUEST['school_id'];
		 	$teacher = M('teacher');
		 	$teacherlist = $teacher->where($tt)->select();
		 	$array3= "";
		 	foreach($teacherlist as $k1=>$l){
		 		$array3[$k1] = $l['user_id'];
		 	}
		 	$data['id'] = array('in',$array3);

		// 	if($_REQUEST['level_id'] != null && $_REQUEST['level_id'] != ""){
		// 		$showlevel = $this->selectlevel($_REQUEST['school_id'],$_REQUEST['level_id']);

		// 		//年级ID
		// 		$tt['level_id'] = $_REQUEST['level_id'];
		// 		$class = M('class');
		// 		$clasinfo = $class->where($tt)->select();
		// 		$array="";
		// 		foreach($clasinfo as $k=>$l){
		// 			$array[$k] = $l['teacher_id'];
		// 		}

		// 		$teacher = M('teacher');
		// 		$tts0['id'] = array('in',$array);
		// 		$teacherlist = $teacher->where($tts0)->select();
		// 		$array2="";
		// 		foreach($teacherlist as $key=>$l){
		// 			$array2[$key] = $l['user_id'];
		// 		}
		// 		$data['id'] = array('in',$array2);;

		// 		if($_REQUEST['class_id'] != null && $_REQUEST['class_id'] != ""){
		// 			$showclass = $this->selectclass($_REQUEST['school_id'],$_REQUEST['level_id'],$_REQUEST['class_id']);

		// 			//班级ID
		// 			$tt['id'] = $_REQUEST['class_id'];
		// 			$class = M('class');
		// 			$clasinfo = $class->where($tt)->find();
		// 			$teacher = M('teacher');
		// 			$tts['id'] = $clasinfo['teacher_id'];
		// 			$tealist = $teacher->where($tts)->find();
		// 			$data['id'] = $tealist['user_id'];
		// 		}else{
		// 			$class = $this->selectclass($_REQUEST['school_id'],$_REQUEST['level_id']);
		// 		}
		// 	}else{
		// 		$level = $this->selectlevel($_REQUEST['school_id']);
		// 	}
		 }else{
		 	$showschool =$this->selectschool();
		 }


		if($_REQUEST['is_teacher'] != null && $_REQUEST['is_teacher'] != ""){
			$data['is_teacher'] =1;
			$real['is_teacher']=1;
			$isteahcer = $this->is_teacher(1);
		}else{
			$isteahcer = $this->is_teacher();
		}

		if($_REQUEST['is_parent'] != null && $_REQUEST['is_parent'] != ""){
			$data['is_parent'] =1;
			$real['is_parent']=1;
			$isparent = $this->is_parent(1);
		}else{
			$isparent = $this->is_parent();
		}

		if($_REQUEST['is_school'] != null && $_REQUEST['is_school'] != ""){
			$data['is_school'] =1;
			$real['is_school']=1;
			$isschool = $this->is_school(1);
		}else{
			$isschool = $this->is_school();
		}

//		print_r($data);
		$m = M('user');
		$this->_listpage($m,$data, '', $asc = false,$real);

		$this->assign('school',$showschool);
		$this->assign('isteacher',$isteahcer);
		$this->assign('isparent',$isparent);
		$this->assign('isschool',$isschool);

//		$this->assign('level',$showlevel);
//		$this->assign('class',$showclass);
		$this->display();
	}


	function add(){
		$str= $this->getschoolname();
		$this->assign('schoolname',$str);
		$this->display();
	}

	function doadd(){
		$sid = $this->_isempty($_POST['school_id'],'请选择学校');
		$XM = $this->_isempty($_POST['XM'],'请填写姓名');
		$XBM = $this->_isempty($_POST['XBM'],'请填写性别');
		$SJHM = $this->_isempty($_POST['SJHM'],'手机不能为空');
		$username = $this->_isempty($_POST['username'],'账号不能为空');
		$password = $this->_isempty($_POST['password'],'密码不能为空');

//		$subjectid = $this->_isempty($_POST['subject_id'],'科目不能为空');
//		$classid = $this->_isempty($_POST['class_id'],'班级不能为空');
//		$powerid = $this->_isempty($_POST['power_id'],'管理班级');

		$map1['username'] = $username;
		$map1['password'] = $password;
		$map1['is_teacher'] = 1;
		$map1['is_school'] = 1;
		$map1['is_parent'] = 0;
		$map1['register_time'] = time();
		$map1['SJHM'] =$SJHM;
		$map1['password_des'] = encrypt($password);
//		$map1['school_id'] = $sid;
		$map1['admin_id'] = $_SESSION['adminid'];
		$map1['shenhe'] = $_POST['shenhe'];
		$map1['status'] = $_POST['status'];

		$map1['audit_opinion'] = $_POST['audit_opinion'];
		$map1['status'] = 0;
		//创建时间
		$map1['addtime']= time();



		$map2['XM'] = $_POST['XM'];
		$map2['XBM'] = $_POST['XBM'];
		if($_FILES['pic']['name'] != null){
			$upload = $this->uploadimg('teacher/');
			$pic = substr($upload[0]['savepath'].$upload[0]['savename'],1);
			$map2['pic'] = $pic;
		}
		$map2['teacher_no'] = $_POST['teacher_no'];
		$map2['LXDH'] = $_POST['LXDH'];
		$map2['LXDZ'] = $_POST['LXDZ'];
		$map2['intro'] = $_POST['intro'];
		$map2['iswork'] = $_POST['iswork'];
		$map2['DZXX'] = $_POST['DZXX'];
		$map2['QQ'] = $_POST['QQ'];
		$map2['school_id'] = $sid;

		if(count($subjectid)>0){
			$subjectid = implode(',',$subjectid);
			$map2['subject_id'] = $subjectid;
		}


		$teacherid = $this->addteacherinfo($sid,$map1,$map2);
		if($teacherid >0){
			add_log($_SESSION['adminid'],'添加 ID:'.$teacherid.'教师',2);
//			$this->updateclass($classid,$teacherid);
//			$this->updateteacherpower($powerid,$teacherid);

			$this->assign('jumpUrl',__APP__.'/Teacher/lists/');
			$this->assign('waitSecond','3');
			$this->success("添加成功");
		}
	}


	function updateclass($ids,$teacherid){
		$set['teacher_id'] = $teacherid;
		$data['id'] = array('in',$ids);
		$m = M('class');
		$isok = $m->where($data)->save($set);
		if($isok){
			add_log($_SESSION['adminid'],'修改 ID:'.$teacherid.'执教班级',2);
		}
	}

	function updateteacherpower($classid,$teacherid){
		$m = M('teacher_power');
		foreach($classid as $k=>$l){
			$data['teacher_id'] = $teacherid;
			$data['class_id'] =$l;
			$m->add($data);
		}
		add_log($_SESSION['adminid'],'修改ID:'.$teacherid.'管理班级',2);
	}


    function addteacherinfo($schoolid,$map1,$map2,$type='teacher') {
		$user = M('user');
		$userlist = $user->where('SJHM="'.$map1[SJHM].'"')->find();

		$userid = 0;
		if($userlist == null){
			$time = time();
			//$map1['addtime'] = $time;
			$map1['endtime'] = $time+(3600*24*365);
			$map1['shenhe']=1;
			$map1['status']=1;
			$userid = $user->add($map1);
		}
		else{
			$data['id'] = $userlist['id'];
			$data['is_teacher'] = 1;
			$user->save($data);
			if($type == 'teacher'){
				$userid = $userlist['id'];
			}

		}

		if($userid == 0){
			$this->error('手机号码为：'.$map1[SJHM].'用户已经存在');
		}

		$tea = M('teacher');
		if($userlist == null){
			$teachid = 0;
			$map2['user_id'] = $userid;
			if($teachid = $tea->add($map2)){
				return $teachid;
			}
		}else{
			$map3['user_id'] = $userid;
			$tealist = $tea->where($map3)->find();
			return $tealist['id'];
		}
    }





    function getschoolname(){
		$m = M('school');
		$list = $m->order('CONVERT(XXMC USING gbk)')->select();
    	$str.="<select id='school_id' name='school_id' class='ui-input'>";
    	$str.="<option value=''>请选择</option>";
    	foreach($list as $k=>$l){
			$str.='<option value="'.$l[id].'">'.$l[XXMC].'</option>';
    	}
		$str.='</select>';
		return $str;
    }


	function getsubject(){
		$sid = $_POST['school_id'];
		$m = M('subject');
		$data['school_id'] = $sid;
		$list = $m->where($data)->select();
		$str1 = '';
		$str1.='<tr class="ui-form-item"><td  class="ui-label">任教科目：</td><td>';
		foreach($list as $k=>$l){
			$str1.='<label for="" class="checklabel"><input class="ui-checkbox" name="subject_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[KCMC].'</label>';
		}
		$str1.='</td></tr>';


//		$m1 = M('class');
//		$map['school_id'] = $sid;
//		$map['teacher_id'] = array('exp','is null');
//		$list1 = $m1->where($map)->select();
//
//		$str2.='';
//		$str2.= '<tr class="ui-form-item"><td  class="ui-label">管理班级：</td><td>';
//		foreach($list1 as $k=>$l){
//			$str2.='<label for="" class="checklabel"><input class="ui-checkbox" name="class_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[class_name].'</label>';
//		}
//		$str2.='</td></tr>';



		$map1['school_id'] = $sid;
		$list2 = $m1->where($map1)->select();

		$str3.= '';
		$str3.= '<tr class="ui-form-item"><td  class="ui-label">执教班级：</td><td>';
		foreach($list2 as $k=>$l){
			$str3.='<label for="" class="checklabel"><input class="ui-checkbox" name="power_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[class_name].'</label>';
		}
		$str3.='</td></tr>';
		echo $str1.$str2.$str3;
	}






	function edit(){
		$id = $this->_isempty($_GET['id'],'ID不能为空');
		$m = M('user');
		$list = $m->find($id);

		if($list['is_teacher'] == 1 ){
			$teacher = M('teacher');
			$data['user_id'] = $list['id'];
			$tlist = $teacher->where($data)->find();
		}

		if($list['is_parent'] == 1){
			$parent = M('parent');
			$data['user_id'] = $list['id'];
			$tlist = $parent->where($data)->find();
		}


		$this->assign('list',$list);
		$this->assign('tlist',$tlist);
		$subjectid = explode(',',$tlist['subject_id']);

//		$subject = $this->_editsubject($list['school_id'],$subjectid);
//		$class = $this->_editclass($list['school_id'],$tlist['id']);
//		$power = $this->_editpower($list['school_id'],$tlist['id']);

//		$this->assign('subject',$subject);
//		$this->assign('class',$class);
//		$this->assign('power',$power);
		$this->display();
	}


	function doedit(){
		$id = $this->_isempty($_POST['id'],'ID 不能为空');
		$teacherid = $this->_isempty($_POST['teacherid'],'TID不能为空');

		$sid = $_POST['school_id'];

		$XM = $this->_isempty($_POST['XM'],'请填写姓名');
		$SJHM = $this->_isempty($_POST['SJHM'],'手机不能为空');
		$username = $this->_isempty($_POST['username'],'账号不能为空');
		$password = $this->_isempty($_POST['password'],'密码不能为空');

		$endtime = $this->_isempty($_POST['endtime'],'截止日期不能为空');
//		$subjectid = $this->_isempty($_POST['subject_id'],'科目不能为空');
//		$classid = $this->_isempty($_POST['class_id'],'班级不能为空');
//		$powerid = $this->_isempty($_POST['power_id'],'管理班级不能为空');

		$user = M('user');
		$userlist = $user->find($id);

		$map0['SJHM'] = $SJHM;
		$isok = $user->where("SJHM='".$SJHM."' and id!=".$id)->count();
		if($isok >0){
			$this->error('手机号码已存在！');
			exit;
		}
		$isok1 = $user->where("username='".$username."' and id!=".$id)->count();
		if($isok >0){
			$this->error('用户名已存在！');
			exit;
		}

		$map1['id'] = $id;
		$map1['username'] = $username;
		$map1['password'] = $password;
		$map1['register_time'] = time();
		$map1['SJHM'] =$SJHM;
		$map1['endtime'] = strtotime($endtime);
		$map1['password_des'] = encrypt($password);
		$map1['shenhe'] = $_POST['shenhe'];
		$map1['status'] = $_POST['status'];
		$map1['audit_opinion'] = $_POST['audit_opinion'];
		$m = M('user');
		$m->save($map1);





		if($userlist['is_teacher'] == 1 ){
			$teacher = M('teacher');
//			$map2['user_id'] = $userlist['id'];
			$map2['id'] = $teacherid;
			$map2['XM'] = $_POST['XM'];
			$map2['XBM'] = $_POST['XBM'];

			$map2['teacher_no'] = $_POST['teacher_no'];
			$map2['LXDH'] = $_POST['LXDH'];
			$map2['LXDZ'] = $_POST['LXDZ'];
			$map2['intro'] = $_POST['intro'];
			$map2['iswork'] = $_POST['iswork'];
			$map2['DZXX'] = $_POST['DZXX'];
			$map2['QQ'] = $_POST['QQ'];
			// $map2['school_id'] = $sid;


			if($_FILES['pic']['name'] != null){
				$upload = $this->uploadimg('teacher/');
				$pic = substr($upload[0]['savepath'].$upload[0]['savename'],1);
				$map2['pic'] = $pic;
				$map2['pic'] = ltrim($pic,'/');
			}
			$tea = M('teacher');
			$tea->save($map2);
		}

		if($userlist['is_parent'] == 1){
			$parent = M('parent');
			$map2['id'] = $teacherid;
//			$map2['user_id'] = $userlist['id'];
			$map2['XM'] = $_POST['XM'];
			$map2['XBM'] = $_POST['XBM'];

			if($_FILES['pic']['name'] != null){
				$upload = $this->uploadimg('parent/');
				$pic = substr($upload[0]['savepath'].$upload[0]['savename'],1);
				$map2['pic'] = $pic;
				$map2['pic'] = ltrim($pic,'/');
			}

			$parent = M('parent');
			$parent->save($map2);
		}


		add_log($_SESSION['adminid'],'修改ID:'.$teacherid.'老师',2);
//		$this->updateclass($classid,$teacherid);
//		$this->updateteacherpower($powerid,$teacherid);

		$this->assign('jumpUrl',__APP__.'/Teacher/lists/');
		$this->assign('waitSecond','1');
		$this->success("修改成功");

	}


	function delete(){
		$userid = $this->_isempty($_GET['id'],'ID不能为空');

		$tea = M('teacher');
		$tt['user_id'] = $userid;
		$teacherlist = $tea->where($tt)->find();
		$id = $teacherlist['id'];

		$lesson = M('lesson');
		$class = M('class');
		$student_work = M('student_work');
		$teacher_power = M('teacher_power');

		$map['teacher_id'] = $id;
		if($lesson->where($map)->find()){

			$this->error('课程中存在该老师数据');
		}


		if($class->where($map)->find()){
			$this->error('班级中存在该老师数据');
		}

		if($student_work->where($map)->find()){
			$this->error('学生作业中存在该老师数据');
		}

		if($teacher_power->where($map)->find()){
			$this->error('教师执教中存在该老师数据');
		}


		$parent = M('parent');
		$pp['user_id'] = $userid;
		$plist = $parent->where($pp)->count();
		if($plist == 0){
			$m = M('user');
//			$userlist = $m->find($userid);
			$m->delete($userid);
		}
		else
		{
			$maps['parent_id'] =$plist['id'];
			$mstudent=M('student');
			if ($mstudent->where($maps)->count()>0)
			{
				$this->error('存在学生的家长不能删除');
			}
			else{
				$parent->where($maps)->delete();	
			}
		}

		$data['user_id'] = $userid;
		$tealist = $tea->where($data)->find();
		$tea->delete($tealist['id']);

		add_log($_SESSION['adminid'],'删除 ID:'.$id.'教师',2);
		$this->assign('jumpUrl',__APP__.'/Teacher/lists/');
		$this->assign('waitSecond','1');
		$this->success("删除成功");
	}


	function teacher_alldelete(){
		$ids = $this->_isempty($_POST['ids'],'ID不能为空');

		$parent = M('parent');
		$m = M('user');
		$tea = M('teacher');

		$lesson = M('lesson');
		$class = M('class');
		$student_work = M('student_work');
		$teacher_power = M('teacher_power');

		foreach($ids as $k=>$userid){
			$tt['user_id'] = $userid;
			$teacherlist = $tea->where($tt)->find();
			$id = $teacherlist['id'];

			$map['teacher_id'] = $id;
			if($lesson->where($map)->find()){

				$this->error('课程中存在该老师数据');
			}

			if($class->where($map)->find()){
				$this->error('班级中存在该老师数据');
			}

			if($student_work->where($map)->find()){
				$this->error('学生作业中存在该老师数据');
			}

			if($teacher_power->where($map)->find()){
				$this->error('教师执教中存在该老师数据');
			}

			add_log($_SESSION['adminid'],'删除 ID:'.$id.'教师',2);

			$pp['user_id'] = $userid;
			$plist = $parent->where($pp)->count();
			if($plist == 0){
				$m->delete($userid);
			}
			else
			{
				$maps['parent_id'] =$plist['id'];
				$mstudent=M('student');
				if ($mstudent->where($maps)->count()>0)
				{
					$this->error('存在学生的家长不能删除');
				}
				else{
					$parent->where($maps)->delete();	
				}
			}

		}

//		$user = M('user');
//		$data['id'] = array('in',$userid);
//		$user->where($data)->delete();

		$tea = M('teacher');
		$data1['user_id'] = array('in',$userid);
		$tea->where($data1)->delete();

		$this->assign('jumpUrl',__APP__.'/Teacher/lists/');
		$this->assign('waitSecond','1');
		$this->success("删除成功");

	}


	function teacher_allshenhe(){
		$ids = $this->_isempty($_POST['ids'],'ID不能为空');
		$m = M('user');
		foreach($ids as $k=>$userid){
			add_log($_SESSION['adminid'],'审核 ID:'.$id,2);
			$m->where(array('id'=>$userid))->save(array('status'=>1));

		}
		$this->assign('jumpUrl',__APP__.'/Teacher/lists/');
		$this->assign('waitSecond','1');
		$this->success("批量审核成功");

	}





	function _editsubject($schoolid,$classid){
		$m = M('subject');
		$data['school_id'] = $schoolid;
		$list = $m->where($data)->select();
		$str1 = '';
		foreach($list as $k=>$l){
			if(in_array($l['id'],$classid)){
				$str1.='<label for="" class="checklabel"><input class="ui-checkbox" name="subject_id[]" id="test" value="'.$l[id].'" type="checkbox" checked>'.$l[KCMC].'</label>';
			}else{
				$str1.='<label for="" class="checklabel"><input class="ui-checkbox" name="subject_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[KCMC].'</label>';
			}
		}
		return $str1;
	}

	function _editclass($schoolid,$tid){
		$m1 = M('class');
		$map['school_id'] = $schoolid;
		$map['teacher_id'] = array(array('exp','is null'),array('eq',$tid),'or');
		$list1 = $m1->where($map)->select();
		foreach($list1 as $k=>$l){
			if($l['teacher_id'] == $tid){
				$str2.='<label for="" class="checklabel"><input class="ui-checkbox" name="class_id[]" id="test" value="'.$l[id].'" type="checkbox" checked>'.$l[class_name].'</label>';
			}else{
				$str2.='<label for="" class="checklabel"><input class="ui-checkbox" name="class_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[class_name].'</label>';
			}
		}
		return $str2;
	}


	private function _editpower($schoolid,$tid){
		$m = M('teacher_power');
		$data['school_id'] = $schoolid;
		$data['teacher_id'] = $tid;
		$list2 = $m->where($data)->select();
		foreach($list2 as $key=>$l){
			$c[$key] = $l['class_id'];
		}

		$class = M('class');
		$map['school_id'] = $schoolid;
		$list = $class->where($map)->select();
		foreach($list as $k=>$l){
			if(in_array($l['id'],$c)){
				$str3.='<label for="" class="checklabel"><input class="ui-checkbox" name="power_id[]" id="test" value="'.$l[id].'" type="checkbox" checked>'.$l[class_name].'</label>';
			}else{
				$str3.='<label for="" class="checklabel"><input class="ui-checkbox" name="power_id[]" id="test" value="'.$l[id].'" type="checkbox">'.$l[class_name].'</label>';
			}
		}
		return $str3;
	}




	public function import(){
		$m = M('school');
		$schlist = $m->order('id desc')->select();
		$this->assign('schlist',$schlist);
		$this->display();
	}


	public function doimport(){
		$school_id = $this->_isempty($_POST['school_id'],'请选择学校');

		$dir = dirname(_FILE_);//获得编译文件当前目录
		$dir = str_replace("\\", "/", $dir)."/";
		$time = time();
		$filename = 'teacher'.$time.'.xls';
		$path = $dir.$filename;
		$path = "./excel/".$filename;
		$result = move_uploaded_file($_FILES['uploadfile']['tmp_name'], $path);
		if ($result){
			Vendor('excel_reader2');//调用第三方类，可以是require_once('指向reader.php文件所在的路径')
			$data = new Spreadsheet_Excel_Reader();
			$data->setOutputEncoding('utf-8');

			error_reporting(E_ALL ^ E_NOTICE);
			$data->read($path);
			$teacher = M('teacher');
			$user = M('user');
			for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
			    $arr = array();
			    for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
			        $arr[] = $data->sheets[0]['cells'][$i][$j];
			    }
			    mysql_query("set names 'utf-8'");

				if($arr[0] !="" || $arr[0] != null){
					$map['XM'] = $arr[0];
				}
				if($arr[1] != "" || $arr[1] !=null){
					$map['XMPY'] = $arr[1];
				}

				if($arr[2] !="" || $arr[2] != null){
					$map['XBM'] = $arr[2];
				}
				if($arr[3] !="" || $arr[3] != null){
					$map['teacher_no'] = $arr[3];
				}

				if($arr[4] !="" || $arr[4] != null){
					$map['LXDH'] = $arr[4];
				}
				if($arr[5] !="" || $arr[5] != null){
					$pp['SJHM'] = $arr[5];
				}
				if($arr[6] !="" || $arr[6] != null){
					$map['LXDZ'] = $arr[6];
				}
				if($arr[7] !="" || $arr[7] != null){
					$map['intro'] = $arr[7];
				}
				if($arr[8] !="" || $arr[8] != null){
					if($arr[8] == '是'){
						$map['iswork'] =1;
					}else if($arr[8] == '否'){
						$map['iswork'] = 0;
					}
				}
				if($arr[9] !="" || $arr[9] != null){
					$pp['username'] = $arr[9];
				}
				if($arr[10] !="" || $arr[10] != null){
					$pp['password'] = $arr[10];
				}
				if($arr[11] !="" || $arr[11] != null){
					$map['DZXX'] = $arr[11];
				}
				if($arr[12] !="" || $arr[12] != null){
					$map['QQ'] = $arr[12];
				}

				if($arr[13] != "" || $arr[13] != null){
					$subject = explode('，',$arr[13]);
					$arrsubid = "";
					foreach($subject as $key=>$s){
						$mapcon['school_id'] = $school_id;
						$mapcon['KCMC'] = $s;
						$sub = M('subject');
						$subinfo = $sub->where($mapcon)->find();
						if($subinfo != null){
							$arrsubid.=$subinfo['id'].',';
						}else{
							$this->error('第'.($i).'行，科目'.$s.'不存在');
						}
					}
					$arrsubid.=0;
					$map['subject_id'] = $arrsubid;
				}


				if($map != null){
					$pp['is_teacher'] = 1;
					$pp['is_school'] = 0;
					$pp['is_parent'] = 0;
					$pp['register_time'] = time();
					$pp['password_des'] = encrypt($pp['password']);
					$pp['school_id'] = $school_id;
					$pp['status'] = 0;
					$pp['shenhe'] = 1;
					//创建时间
					$pp['addtime']= time();

					$map['school_id'] = $school_id;

					$id = $this->addteacherinfo($school_id,$pp,$map);

					if($arr[14] != "" || $arr[14] != null){
						$class = M('class');
						$mapc['school_id'] = $school_id;
						$mapc['class_name'] = $arr[14];
						$classinfo = $class->where($mapc)->find();
						if($classinfo != null){
							$whe['id'] = $classinfo['id'];
							$set['teacher_id'] = $id;
							$class->where($whe)->save($set);
						}else{
							$this->error('第'.($i).'行，班级'.$arr[14].'不存在');
						}
					}

					if($arr[15] != "" || $arr[15] != null){
						$cname = explode('，',$arr[15]);
						$class = M('class');
						foreach($cname as $k=>$c){
							$mapc1['school_id'] = $school_id;
							$mapc1['class_name'] = $c;
							$classinfo = $class->where($mapc1)->find();
							if($classinfo != null){
								$ins['class_id'] = $classinfo['id'];
								$ins['teacher_id'] = $id;
								$tp = M('teacher_power');
								$tp->add($ins);
							}else{
								$this->error('第'.($i).'行，班级'.$c.'不存在');
							}
						}
					}

					if($id == 0){
						$this->error('第'.($i).'行，用户:'.$pp['username'].'导入失败');
					}
				}
			}
//			echo "<a href='".__URL__."/import'>返回</a>";
//			$this->assign('waitSecond','1');
			$this->success('导入成功');
		}else{
		    $this->error("上传失败！");
		}
	}


	function selectschool($val){
		$m = M('school');
		$list = $m->order('id desc')->select();
		$str='<select name="school_id" id="school_id">';
		$str.='<option value="">请选择</option>';
		foreach($list as $k=>$l){
			if($val == $l['id']){
				$str.='<option value='.$l["id"].' selected>'.$l["XXMC"].'</option>';
			}else{
				$str.='<option value='.$l["id"].'>'.$l["XXMC"].'</option>';
			}
		}
		$str.='</select>';
		return $str;
	}

	function selectlevel($schoolid,$levelid){
		$school = M('school');
		$schoollist = $school->find($schoolid);
		$data['school_type_id'] = $schoollist['school_type_id'];
		$m = M('level');
		$levellist = $m->where($data)->select();
		$str.='<option value="">请选择</option>';
		foreach($levellist as $k=>$l){
			if($l['id'] == $levelid){
//				echo $l['id'];
				$str.='<option value='.$l["id"].' selected>'.$l["level_name"].'</option>';
			}else{

				$str.='<option value='.$l["id"].'>'.$l["level_name"].'</option>';
			}
		}
		return $str;
	}

	function selectclass($schoolid,$levelid,$classid){
		$class = M('class');
		$data['school_id'] = $schoolid;
		$data['level_id'] = $levelid;
		$classlist =$class->where($data)->select();
//		echo $class->getlastsql();
		$str.='<option value="">请选择</option>';
		foreach($classlist as $l){
			if($l['id'] == $classid){
				$str.='<option value='.$l["id"].' selected>'.$l["class_name"].'</option>';
			}else{
				$str.='<option value='.$l["id"].'>'.$l["class_name"].'</option>';
			}
		}
		return $str;
	}




	function showlevel(){
		$school_id = $_POST['school_id'];
		$m = M('school');
		$schoolinfo = $m->find($school_id);
		$level = M('level');
		$lel['school_type_id'] = $schoolinfo['school_type_id'];
		$levlist = $level->where($lel)->select();
		$str.='<option value="">请选择</option>';
		foreach($levlist as $k=>$l){
			$str.='<option value='.$l["id"].'>'.$l["level_name"].'</option>';
		}
		echo $str;
	}


	function showclass(){
		$school_id = $_POST['school_id'];
		$level_id = $_POST['level_id'];
		$m = M('class');
		$data['school_id'] = $school_id;
		$data['level_id'] = $level_id;
		$classlist = $m->where($data)->select();
//		echo $m->getlastsql();
		$str.='<option value="">请选择</option>';
		foreach($classlist as $k=>$l){
			$str.='<option value='.$l["id"].'>'.$l["class_name"].'</option>';
		}
		echo $str;
	}

//	function showusertype($arr){
//		if(in_array('is_teacher',$arr)){
//			$str1.='<input type="checkbox" name="is_teacher" value="1" checked />老师&nbsp;&nbsp;';
//		}else{
//			$str1.='<input type="checkbox" name="is_teacher" value="1" />老师&nbsp;&nbsp;';
//		}
//
//		if(in_array('is_parent',$arr)){
//			$str2.='<input type="checkbox" name="is_parent" value="1" checked />家长&nbsp;&nbsp;';
//		}else{
//			$str2.='<input type="checkbox" name="is_parent" value="1" />家长&nbsp;&nbsp;';
//		}
//
//		if(in_array('is_school',$arr)){
//			$str3.='<input type="checkbox" name="is_school" value="1" checked />学校管理员&nbsp;&nbsp;';
//		}else{
//			$str3.='<input type="checkbox" name="is_school" value="1" />学校管理员&nbsp;&nbsp;';
//		}
//
//		$str=$str1.$str2.$str3;
//		return $str;
//	}


	function is_teacher($yes){
		if($yes == 1){
			$str1.='<input type="checkbox" name="is_teacher" value="1" checked />老师&nbsp;&nbsp;';
		}else{
			$str1.='<input type="checkbox" name="is_teacher" value="1" />老师&nbsp;&nbsp;';
		}
		return $str1;
	}

	function is_parent($yes){
		if($yes == 1){
			$str2.='<input type="checkbox" name="is_parent" value="1" checked />家长&nbsp;&nbsp;';
		}else{
			$str2.='<input type="checkbox" name="is_parent" value="1" />家长&nbsp;&nbsp;';
		}
		return $str2;
	}

	function is_school($yes){
		if($yes == 1){
			$str3.='<input type="checkbox" name="is_school" value="1" checked />学校管理员&nbsp;&nbsp;';
		}else{
			$str3.='<input type="checkbox" name="is_school" value="1" />学校管理员&nbsp;&nbsp;';
		}
		return $str3;
	}

















}
?>